package com.example.controller;

import com.example.common.JWTInterceptor;
import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.User;
import com.example.exception.CustomerException;
import com.example.service.AdminService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class WebController {
    @Autowired
    JWTInterceptor jwtInterceptor;
    @Autowired
   AdminService adminService;
    @Autowired
    UserService userService;

/*
// 表示这是一个get请求的接口
    @GetMapping("/hello") //接口的路径,全局唯一的
    public Result hello() {
            int a = 1/1;
        return Result.success("hello111");//接口的返回值
    }
*/

    @GetMapping("/admin")
    public Result admin(String name) {
        String admin = adminService.admin(name);
        return Result.success(admin);
    }
    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        Account dbAccount = null;
        if ("ADMIN".equals(account.getRole())) {
            dbAccount = adminService.login(account);
        }else if ("USER".equals(account.getRole())) {
            dbAccount = userService.login(account);
        }else {
            throw new CustomerException("非法请求");
        }
                return Result.success(dbAccount);
    }
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        userService.register(user);
        return Result.success();
    }
}
